// FrontEnd Plus GUI for JAD
// DeCompiled : JHibernateColumnSettingsPanel.class

package middlegen.plugins.hibernate.swing;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import middlegen.Column;
import middlegen.ColumnDecorator;
import middlegen.javax.JavaColumn;
import middlegen.javax.Sql2Java;
import middlegen.plugins.hibernate.HibernateColumn;
import middlegen.plugins.hibernate.HibernateTable;
import middlegen.plugins.hibernate.interfaces.JavaTypeMapper;
import middlegen.swing.BooleanNodeCheckBox;
import middlegen.swing.JColumnSettingsPanel;

public class JHibernateColumnSettingsPanel extends JColumnSettingsPanel
{

    protected HibernateColumn _currentColumns[];
    private final JLabel _columnNameLabel = new JLabel("  Java property name");
    private final JTextField _columnNameField = new JTextField();
    private final JLabel _columnTypeLabel = new JLabel("  Java type");
    private final JComboBox _columnTypeCombo = new JComboBox();
    private final JRadioButton _accessFieldRadioButton = new JRadioButton("field");
    private final JRadioButton _accessPropertyRadioButton = new JRadioButton("property");
    private final JRadioButton _publicGetRadioButton = new JRadioButton("public");
    private final JRadioButton _protectedGetRadioButton = new JRadioButton("protected");
    private final JRadioButton _privateGetRadioButton = new JRadioButton("private");
    private final JRadioButton _publicSetRadioButton = new JRadioButton("public");
    private final JRadioButton _protectedSetRadioButton = new JRadioButton("protected");
    private final JRadioButton _privateSetRadioButton = new JRadioButton("private");
    private final JRadioButton _publicFieldRadioButton = new JRadioButton("public");
    private final JRadioButton _protectedFieldRadioButton = new JRadioButton("protected");
    private final JRadioButton _privateFieldRadioButton = new JRadioButton("private");
    private final JLabel _columnSpecialtyLabel = new JLabel("  Hibernate mapping specialty");
    private final JComboBox _columnSpecialtyCombo = new JComboBox();
    private final BooleanNodeCheckBox _columnUpdateableCheckBox = new BooleanNodeCheckBox("Column updateable", null);
    private final BooleanNodeCheckBox _columnInsertableCheckBox = new BooleanNodeCheckBox("Column insertable", null);
    private final JLabel _fieldDescriptionLabel = new JLabel("  Field description");
    private final JTextField _fieldDescriptionField = new JTextField();
    private final JLabel _fieldAccessMethodLabel = new JLabel("  Field access method");
    private final JLabel _propertyGetScopeLabel = new JLabel("  Property get scope");
    private final JLabel _propertySetScopeLabel = new JLabel("  Property set scope");
    private final JLabel _fieldScopeLabel = new JLabel("  Field set scope");
    private final BooleanNodeCheckBox _includeInEqualsCheckBox = new BooleanNodeCheckBox("Include in Equals/Hash", null);
    private final BooleanNodeCheckBox _includeInTostringCheckBox = new BooleanNodeCheckBox("Include in toString", null);
    private final BooleanNodeCheckBox _genPropertyCheckBox = new BooleanNodeCheckBox("Generate the property", null);
    private final JLabel _beanPropertyTypeLabel = new JLabel("  Bean constraint property type");
    private final JComboBox _beanPropertyTypeCombo = new JComboBox();
    private final DocumentListener _fieldDescriptionDocumentListener = new DocumentListener() {

        public void changedUpdate(DocumentEvent event)
        {
            update();
        }

        public void insertUpdate(DocumentEvent event)
        {
            update();
        }

        public void removeUpdate(DocumentEvent event)
        {
            update();
        }

        private void update()
        {
            for(int i = 0; i < _currentColumns.length; i++)
                _currentColumns[i].setFieldDescription(_fieldDescriptionField.getText());

        }

    };
    private final ActionListener _comboListener = new ActionListener() {

        public void actionPerformed(ActionEvent event)
        {
            update();
        }

        private void update()
        {
            String type = (String)_columnTypeCombo.getSelectedItem();
            if(type != null)
            {
                for(int i = 0; i < _currentColumns.length; i++)
                {
                    String acceptableTypes[] = Sql2Java.getJavaTypes(_currentColumns[i].getSqlType());
                    boolean yesOK = false;
                    int j = 0;
                    do
                    {
                        if(j >= acceptableTypes.length)
                            break;
                        if(acceptableTypes[j].equals(type))
                        {
                            yesOK = true;
                            break;
                        }
                        j++;
                    } while(true);
                    if(yesOK)
                    {
                        _currentColumns[i].setJavaType(type);
                        continue;
                    }
                    JavaTypeMapper javaTypeMapper = _currentColumns[i].getJavaTypeMapper();
                    if(javaTypeMapper == null)
                        _currentColumns[i].setJavaType(Sql2Java.getPreferredJavaType(_currentColumns[i].getSqlType(), 0, 0));
                    else
                        _currentColumns[i].setJavaType(javaTypeMapper.getPreferredJavaType(_currentColumns[i]));
                }

            }
        }

    };
    private final ActionListener _beanComboListener = new ActionListener() {

        public void actionPerformed(ActionEvent event)
        {
            update();
        }

        private void update()
        {
            for(int i = 0; i < _currentColumns.length; i++)
            {
                String type = (String)_beanPropertyTypeCombo.getSelectedItem();
                if(type != null)
                    _currentColumns[i].setBeanPropertyType(type);
            }

        }

    };
    private final ActionListener _columnSpecialtyComboListener = new ActionListener() {

        public void actionPerformed(ActionEvent event)
        {
            update();
        }

        private void update()
        {
            for(int i = 0; i < _currentColumns.length; i++)
            {
                String type = (String)_columnSpecialtyCombo.getSelectedItem();
                if(type != null)
                    _currentColumns[i].setColumnSpecialty(type);
            }

        }

    };
    private final ActionListener toggleBtnUpdateableAction = new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            for(int i = 0; i < _currentColumns.length; i++)
                _currentColumns[i].setUpdateable(((BooleanNodeCheckBox)e.getSource()).isSelected());

        }

    };
    private final ActionListener toggleBtnInsertableAction = new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            for(int i = 0; i < _currentColumns.length; i++)
                _currentColumns[i].setInsertable(((BooleanNodeCheckBox)e.getSource()).isSelected());

        }

    };
    private final ActionListener toggleBtnIncludeToStringAction = new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            for(int i = 0; i < _currentColumns.length; i++)
                _currentColumns[i].setIncludeToString(((BooleanNodeCheckBox)e.getSource()).isSelected());

        }

    };
    private final ActionListener toggleBtnIncludeEqualsAction = new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            for(int i = 0; i < _currentColumns.length; i++)
                _currentColumns[i].setIncludeEquals(((BooleanNodeCheckBox)e.getSource()).isSelected());

        }

    };
    private final ActionListener toggleBtnGenPropertyAction = new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            for(int i = 0; i < _currentColumns.length; i++)
                _currentColumns[i].setGenProperty(((BooleanNodeCheckBox)e.getSource()).isSelected());

        }

    };

    public JHibernateColumnSettingsPanel()
    {
        setLayout(new BoxLayout(this, 0));
        initComponents();
        addComponents();
        assignAllListeners();
    }

    public void setColumns(Column columns[])
    {
        deassignAllListeners();
        _currentColumns = new HibernateColumn[columns.length];
        for(int i = 0; i < columns.length; i++)
            _currentColumns[i] = (HibernateColumn)columns[i];

        boolean singleColumnSelected = _currentColumns.length == 1;
        if(_currentColumns.length >= 1)
        {
            _columnUpdateableCheckBox.setBooleanNode(_currentColumns[0].getUpdateableNode());
            _columnInsertableCheckBox.setBooleanNode(_currentColumns[0].getInsertableNode());
            _includeInTostringCheckBox.setBooleanNode(_currentColumns[0].getIncludeToStringNode());
            _genPropertyCheckBox.setBooleanNode(_currentColumns[0].getGenPropertyNode());
            _includeInEqualsCheckBox.setBooleanNode(_currentColumns[0].getIncludeEqualsNode());
            _fieldDescriptionField.setText(_currentColumns[0].getFieldDescription());
            _beanPropertyTypeCombo.setSelectedItem(_currentColumns[0].getBeanPropertyType());
            setAccessField();
            setFieldScope();
            setFieldGetScope();
            setFieldSetScope();
        }
        _columnNameField.setEnabled(singleColumnSelected);
        _genPropertyCheckBox.setEnabled(singleColumnSelected);
        if(singleColumnSelected)
            _columnNameField.setText(_currentColumns[0].getVariableName());
        else
            _columnNameField.setText("");
        setHibernateJavaTypeCombo();
        setHibernateSpecialtyCombo();
        assignAllListeners();
    }

    protected void initComponents()
    {
        String sitems[] = {
            "key", "compound key", "version", "timestamp", "property"
        };
        DefaultComboBoxModel sModel = new DefaultComboBoxModel(sitems);
        _columnSpecialtyCombo.setModel(sModel);
        _columnSpecialtyCombo.setEditable(false);
        String bitems[] = {
            "unbound", "bound", "constrained"
        };
        DefaultComboBoxModel cModel = new DefaultComboBoxModel(bitems);
        _beanPropertyTypeCombo.setModel(cModel);
        _beanPropertyTypeCombo.setEditable(false);
        _columnNameField.setEditable(false);
    }

    protected void addComponents()
    {
        JPanel firstGroup = new JPanel();
        firstGroup.setLayout(new BoxLayout(firstGroup, 1));
        firstGroup.setBorder(BorderFactory.createTitledBorder("Column Mapping Attributes"));
        JPanel secondGroup = new JPanel();
        secondGroup.setLayout(new BoxLayout(secondGroup, 1));
        secondGroup.setBorder(BorderFactory.createTitledBorder("Domain Property Meta Attributes"));
        JPanel firstSectionGroup1 = new JPanel();
        firstSectionGroup1.setLayout(new GridLayout(0, 2, 4, 2));
        JPanel secondSectionGroup1 = new JPanel();
        secondSectionGroup1.setLayout(new GridLayout(0, 2, 4, 2));
        JPanel thirdSectionGroup1 = new JPanel();
        thirdSectionGroup1.setLayout(new GridLayout(0, 2, 4, 2));
        JPanel firstSectionGroup2 = new JPanel();
        firstSectionGroup2.setLayout(new GridLayout(0, 2, 4, 2));
        JPanel secondSectionGroup2 = new JPanel();
        secondSectionGroup2.setLayout(new GridLayout(0, 2, 0, 2));
        JPanel thirdSectionGroup2 = new JPanel();
        thirdSectionGroup2.setLayout(new GridLayout(0, 2, 4, 2));
        JPanel fourthSectionGroup2 = new JPanel();
        fourthSectionGroup2.setLayout(new GridLayout(0, 3, 4, 2));
        firstSectionGroup1.add(_columnSpecialtyLabel);
        firstSectionGroup1.add(_columnSpecialtyCombo);
        firstSectionGroup1.add(_columnNameLabel);
        firstSectionGroup1.add(_columnNameField);
        firstSectionGroup1.add(_columnTypeLabel);
        firstSectionGroup1.add(_columnTypeCombo);
        secondSectionGroup1.add(_fieldAccessMethodLabel);
        secondSectionGroup1.add(BuildAccessPanel());
        thirdSectionGroup1.add(_columnUpdateableCheckBox);
        thirdSectionGroup1.add(_columnInsertableCheckBox);
        firstGroup.add(firstSectionGroup1);
        firstGroup.add(secondSectionGroup1);
        firstGroup.add(thirdSectionGroup1);
        firstSectionGroup2.add(_fieldDescriptionLabel);
        firstSectionGroup2.add(_fieldDescriptionField);
        secondSectionGroup2.add(_propertyGetScopeLabel);
        secondSectionGroup2.add(BuildTableGetScopePanel());
        secondSectionGroup2.add(_propertySetScopeLabel);
        secondSectionGroup2.add(BuildTableSetScopePanel());
        secondSectionGroup2.add(_fieldScopeLabel);
        secondSectionGroup2.add(BuildTableFieldScopePanel());
        thirdSectionGroup2.add(_beanPropertyTypeLabel);
        thirdSectionGroup2.add(_beanPropertyTypeCombo);
        fourthSectionGroup2.add(_genPropertyCheckBox);
        fourthSectionGroup2.add(_includeInTostringCheckBox);
        fourthSectionGroup2.add(_includeInEqualsCheckBox);
        secondGroup.add(firstSectionGroup2);
        secondGroup.add(secondSectionGroup2);
        secondGroup.add(thirdSectionGroup2);
        secondGroup.add(fourthSectionGroup2);
        add(firstGroup);
        add(secondGroup);
    }

    private void setHibernateJavaTypeCombo()
    {
        _columnTypeCombo.setEnabled(true);
        _columnTypeCombo.removeAllItems();
        if(_currentColumns[0].isPk() && ((HibernateTable)_currentColumns[0].getTable()).isCompositeKey())
        {
            if(_currentColumns.length > 1)
                _columnTypeCombo.setEnabled(false);
            else
                _columnTypeLabel.setText(String.valueOf(String.valueOf((new StringBuffer("  Java type   (")).append(((HibernateTable)_currentColumns[0].getTable()).getCompoundKeyDestinationClassName()).append(")"))));
        } else
        {
            _columnTypeLabel.setText("  Java type");
        }
        String selectedJavaType = _currentColumns[0].getJavaType();
        String common[] = Sql2Java.getJavaTypes(_currentColumns[0].getSqlType());
        for(int i = 0; i < common.length; i++)
            _columnTypeCombo.addItem(common[i]);

        _columnTypeCombo.setSelectedItem(selectedJavaType);
    }

    private void setHibernateSpecialtyCombo()
    {
        _columnSpecialtyCombo.setEnabled(true);
        _columnSpecialtyCombo.removeAllItems();
        if(_currentColumns.length == 1)
        {
            if(_currentColumns[0].isPk())
            {
                if(((HibernateTable)_currentColumns[0].getTable()).isCompositeKey())
                    _columnSpecialtyCombo.addItem("compound key");
                else
                    _columnSpecialtyCombo.addItem("key");
            } else
            if(_currentColumns[0].isFk())
            {
                _columnSpecialtyCombo.addItem("foreign key");
            } else
            {
                String ptype = _currentColumns[0].getJavaType();
                if(ptype == null)
                {
                    JavaTypeMapper javaTypeMapper = _currentColumns[0].getJavaTypeMapper();
                    if(javaTypeMapper == null)
                        ptype = Sql2Java.getPreferredJavaType(_currentColumns[0].getSqlType(), 0, 0);
                    else
                        ptype = javaTypeMapper.getPreferredJavaType(_currentColumns[0]);
                }
                _columnSpecialtyCombo.addItem("property");
                if(ptype.equals("long") || ptype.equals("int") || ptype.equals("short"))
                    _columnSpecialtyCombo.addItem("version");
                else
                if(ptype.equals("java.sql.Timestamp"))
                    _columnSpecialtyCombo.addItem("timestamp");
            }
            _columnSpecialtyCombo.setSelectedItem(_currentColumns[0].getColumnSpecialty());
            _currentColumns[0].setColumnSpecialty((String)_columnSpecialtyCombo.getSelectedItem());
        } else
        {
            _columnSpecialtyCombo.addItem("property");
            _columnSpecialtyCombo.setEnabled(false);
        }
    }

    private void setFieldScope()
    {
        _publicFieldRadioButton.setSelected(_currentColumns[0].getFieldScope().equals("public"));
        _protectedFieldRadioButton.setSelected(_currentColumns[0].getFieldScope().equals("protected"));
        _privateFieldRadioButton.setSelected(_currentColumns[0].getFieldScope().equals("private"));
    }

    private void setAccessField()
    {
        _accessFieldRadioButton.setSelected(_currentColumns[0].getAccessField().equals("field"));
        _accessPropertyRadioButton.setSelected(_currentColumns[0].getAccessField().equals("property"));
    }

    private void setFieldGetScope()
    {
        _publicGetRadioButton.setSelected(_currentColumns[0].getFieldGetScope().equals("public"));
        _protectedGetRadioButton.setSelected(_currentColumns[0].getFieldGetScope().equals("protected"));
        _privateGetRadioButton.setSelected(_currentColumns[0].getFieldGetScope().equals("private"));
    }

    private void setFieldSetScope()
    {
        _publicSetRadioButton.setSelected(_currentColumns[0].getFieldSetScope().equals("public"));
        _protectedSetRadioButton.setSelected(_currentColumns[0].getFieldSetScope().equals("protected"));
        _privateSetRadioButton.setSelected(_currentColumns[0].getFieldSetScope().equals("private"));
    }

    private JPanel BuildAccessPanel()
    {
        JPanel spanel = new JPanel();
        spanel.setLayout(new FlowLayout());
        ButtonGroup group = new ButtonGroup();
        group.add(_accessFieldRadioButton);
        group.add(_accessPropertyRadioButton);
        ActionListener radioAccessBtnAction = new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                for(int i = 0; i < _currentColumns.length; i++)
                    _currentColumns[i].setAccessField(e.getActionCommand());

            }

        };
        _accessFieldRadioButton.setActionCommand("field");
        _accessFieldRadioButton.addActionListener(radioAccessBtnAction);
        _accessPropertyRadioButton.addActionListener(radioAccessBtnAction);
        _accessPropertyRadioButton.setActionCommand("property");
        spanel.add(_accessFieldRadioButton);
        spanel.add(_accessPropertyRadioButton);
        return spanel;
    }

    private JPanel BuildTableGetScopePanel()
    {
        JPanel spanel = new JPanel();
        spanel.setLayout(new FlowLayout());
        ButtonGroup group = new ButtonGroup();
        group.add(_publicGetRadioButton);
        group.add(_protectedGetRadioButton);
        group.add(_privateGetRadioButton);
        ActionListener radioGetBtnAction = new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                for(int i = 0; i < _currentColumns.length; i++)
                    _currentColumns[i].setFieldGetScope(e.getActionCommand());

            }

        };
        _publicGetRadioButton.setActionCommand("public");
        _publicGetRadioButton.addActionListener(radioGetBtnAction);
        _protectedGetRadioButton.addActionListener(radioGetBtnAction);
        _protectedGetRadioButton.setActionCommand("protected");
        _privateGetRadioButton.addActionListener(radioGetBtnAction);
        _privateGetRadioButton.setActionCommand("private");
        spanel.add(_publicGetRadioButton);
        spanel.add(_protectedGetRadioButton);
        spanel.add(_privateGetRadioButton);
        return spanel;
    }

    private JPanel BuildTableSetScopePanel()
    {
        JPanel spanel = new JPanel();
        spanel.setLayout(new FlowLayout());
        ButtonGroup group = new ButtonGroup();
        group.add(_publicSetRadioButton);
        group.add(_protectedSetRadioButton);
        group.add(_privateSetRadioButton);
        ActionListener radioSetBtnAction = new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                for(int i = 0; i < _currentColumns.length; i++)
                    _currentColumns[i].setFieldSetScope(e.getActionCommand());

            }

        };
        _publicSetRadioButton.setActionCommand("public");
        _publicSetRadioButton.addActionListener(radioSetBtnAction);
        _protectedSetRadioButton.addActionListener(radioSetBtnAction);
        _protectedSetRadioButton.setActionCommand("protected");
        _privateSetRadioButton.addActionListener(radioSetBtnAction);
        _privateSetRadioButton.setActionCommand("private");
        spanel.add(_publicSetRadioButton);
        spanel.add(_protectedSetRadioButton);
        spanel.add(_privateSetRadioButton);
        return spanel;
    }

    private JPanel BuildTableFieldScopePanel()
    {
        JPanel spanel = new JPanel();
        spanel.setLayout(new FlowLayout());
        ButtonGroup group = new ButtonGroup();
        group.add(_publicFieldRadioButton);
        group.add(_protectedFieldRadioButton);
        group.add(_privateFieldRadioButton);
        ActionListener radioFieldBtnAction = new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                for(int i = 0; i < _currentColumns.length; i++)
                    _currentColumns[i].setFieldScope(e.getActionCommand());

            }

        };
        _publicFieldRadioButton.setActionCommand("public");
        _publicFieldRadioButton.addActionListener(radioFieldBtnAction);
        _protectedFieldRadioButton.addActionListener(radioFieldBtnAction);
        _protectedFieldRadioButton.setActionCommand("protected");
        _privateFieldRadioButton.addActionListener(radioFieldBtnAction);
        _privateFieldRadioButton.setActionCommand("private");
        spanel.add(_publicFieldRadioButton);
        spanel.add(_protectedFieldRadioButton);
        spanel.add(_privateFieldRadioButton);
        return spanel;
    }

    private Object[] common(Object arrays[][])
    {
        HashMap union = new HashMap();
        for(int i = 0; i < arrays.length; i++)
        {
            Object array[] = arrays[i];
            for(int j = 0; j < array.length; j++)
            {
                Integer occurrances = (Integer)union.get(array[j]);
                if(occurrances == null)
                    occurrances = new Integer(0);
                union.put(array[j], new Integer(occurrances.intValue() + 1));
            }

        }

        ArrayList common = new ArrayList();
        if(arrays.length > 1)
            common.add("");
        Iterator iterator = union.entrySet().iterator();
        do
        {
            if(!iterator.hasNext())
                break;
            java.util.Map.Entry entry = (java.util.Map.Entry)iterator.next();
            Integer occurrances = (Integer)entry.getValue();
            if(occurrances.intValue() == arrays.length)
                common.add(entry.getKey());
        } while(true);
        Object result[] = common.toArray();
        Arrays.sort(result, Sql2Java.getTypeComparator());
        return result;
    }

    private void assignAllListeners()
    {
        _columnTypeCombo.addActionListener(_comboListener);
        _fieldDescriptionField.getDocument().addDocumentListener(_fieldDescriptionDocumentListener);
        _beanPropertyTypeCombo.addActionListener(_beanComboListener);
        _columnSpecialtyCombo.addActionListener(_columnSpecialtyComboListener);
        _columnUpdateableCheckBox.addActionListener(toggleBtnUpdateableAction);
        _columnInsertableCheckBox.addActionListener(toggleBtnInsertableAction);
        _includeInTostringCheckBox.addActionListener(toggleBtnIncludeToStringAction);
        _genPropertyCheckBox.addActionListener(toggleBtnGenPropertyAction);
        _includeInEqualsCheckBox.addActionListener(toggleBtnIncludeEqualsAction);
    }

    private void deassignAllListeners()
    {
        _columnTypeCombo.removeActionListener(_comboListener);
        _fieldDescriptionField.getDocument().removeDocumentListener(_fieldDescriptionDocumentListener);
        _beanPropertyTypeCombo.removeActionListener(_beanComboListener);
        _columnSpecialtyCombo.removeActionListener(_columnSpecialtyComboListener);
        _columnUpdateableCheckBox.removeActionListener(toggleBtnUpdateableAction);
        _columnInsertableCheckBox.removeActionListener(toggleBtnInsertableAction);
        _includeInTostringCheckBox.removeActionListener(toggleBtnIncludeToStringAction);
        _genPropertyCheckBox.removeActionListener(toggleBtnGenPropertyAction);
        _includeInEqualsCheckBox.removeActionListener(toggleBtnIncludeEqualsAction);
    }




}
